<?php 
/*
* StiPHPcms, A web Content management System made with PHP/MySQL
* JordSti : jord52@gmail.com
* Version : 0.0.2
*
* Copyright (C) 2009  jord52@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

function ShowAvatar($url,$settings)
{
	$max_x = $settings['avatar_max_x'];
	$max_y = $settings['avatar_max_y'];
	
	if(strlen($url)==0)
	{
		$url = "images/no_avatar.jpg";
		return '<img src="'.$url.'" width="'.$max_x.'" height="'.$max_y.'" />';
	}
	
	$size = getimagesize($url);
	$x = $size[0];
	$y = $size[1];
	
	if($x <= $max_x && $y <= $max_y)
	{
		$img_x = $x;
		$img_y = $y;
	}
	else if($x == $y)
	{
		$img_x = $max_x;
		$img_y = $max_y;
	}
	else if($x > $y)
	{
		$img_x = $max_x;
		$img_y = (($img_x*$y)/$x);
	}
	else if($y > $x)
	{
		$img_y = $max_y;
		$img_x = (($img_y*$x)/$y);
	}
	
	$html = '<img src="'.$url.'" width="'.$img_x.'" height="'.$img_y.'" />';
	return $html;
}

class Profile {
	private $cms;
	
	public function __construct($cms)
	{
		$this->cms = $cms;
	}
	
	public function updateProfile()
	{
		require_once("core/tools/string.php");
		require_once("core/tools/date.php");
		
		if(!$this->cms->isUserLog())
		{
			$this->cms->error('','index.php');
		}
		
		$user_id = $_SESSION['user_id'];
		
		$_POST = array_htmlentities($_POST);
		$_POST = array_sql_escape($_POST);

		$name = $_POST['name'];
		$avatar = $_POST['avatar'];
		$email = $_POST['email'];
		$msn = $_POST['msn'];
		$website = $_POST['website'];
		$forum_sign = $_POST['forum_signature'];
		
		$country = $_POST['country'];
		$city = $_POST['city'];
		$work = $_POST['work'];
		$birthdate = getDateField();
		
		$this->cms->sql_query("UPDATE %prefix%profiles SET avatar='$avatar',profile_name='$name',email='$email',msn='$msn',forum_signature='$forum_sign',country='$country',city='$city',work='$work',website='$website',birthdate='$birthdate' WHERE user_id=$user_id");
		
	}
	
	public function showField($data)
	{
		if(strlen($data)==0)
		{
			return $this->cms->lang['n/a'];
		}
		else
		{
			if(ereg('http://',$data))
			{
				$data = '<a href="'.$data.'" target="_blank">'.$data.'</a>';
			}
			
			return $data;
		}
	}
	
	public function showProfile($user_id)
	{
		$user_id = intval($user_id);
		
		$dt = $this->cms->sql_query("SELECT p.user_id,p.profile_name,p.avatar,p.email,p.msn,p.website,p.work,p.country,p.city,p.birthdate,u.username FROM %prefix%profiles p JOIN %prefix%users u ON(u.id=p.user_id) WHERE p.user_id=$user_id");
		
		if(mysql_num_rows($dt)==0)
		{
			$this->cms->error($this->cms->lang['profile_not_found']);
		}
		
		$data = mysql_fetch_array($dt);
		
		if($data['birthdate']==-2145898800)
		{
			$age = $this->cms->lang['n/a'];
			$birthdate = $this->cms->lang['n/a'];
		}
		else
		{
			$stamp = time() - $data['birthdate'];
			$age = $stamp/(365*(24*60*60));
			
			$age = floor($age);
			
			$birthdate = date('d/m/Y',$data['birthdate']);
		}
		
		$_SESSION['profile_email'] = $data['email'];
		$_SESSION['profile_msn'] = $data['msn'];
		
		$html = '<table width="80%" border="0">
  				<tr>
    			<td width="30%">'.ShowAvatar($data['avatar'],$this->cms->settings).'
    			<p><strong>'.$this->cms->lang['profile_name'].'</strong> : '.$this->showField($data['profile_name']).'</p>
    			<p><strong>'.$this->cms->lang['username'].'</strong> : '.$this->showField($data['username']).'</p>
    			</td>
    			<td width="60%"><h3>'.$this->cms->lang['profile_general_info'].'</h3>
    			<p><strong>'.$this->cms->lang['profile_email'].'</strong> : <img src="email_image.php"></p>
    			<p><strong>'.$this->cms->lang['profile_msn_im'].'</strong> : <img src="email_image.php?type=msn"></p>
    			<p><strong>'.$this->cms->lang['profile_website'].'</strong> : '.$this->showField($data['website']).'</p>
    			</td>
  				</tr>
  				<tr>
    			<td>&nbsp;</td>
    			<td>
    			<h3>'.$this->cms->lang['profile_personnal_info'].'</h3>
    			<p><strong>'.$this->cms->lang['profile_work'].'</strong> : '.$this->showField($data['work']).'</p>
    			<p><strong>'.$this->cms->lang['profile_country'].'</strong> : '.$this->showField($data['country']).'</p>
    			<p><strong>'.$this->cms->lang['profile_city'].'</strong> : '.$this->showField($data['city']).'</p>
    			<p><strong>'.$this->cms->lang['profile_age'].'</strong> : '.$age.'</p>
    			</td>
  				</tr>
				</table>';
		
		//<p><strong>'.$this->cms->lang['profile_birthdate'].'</strong> : '.$birthdate.'</p>
		
		$this->cms->renderHeader($this->cms->lang['profile_title'].' : '.$data['username']);
		$this->cms->renderMenu();
		$this->cms->template->addFrame($data['username'],$html);
	}
	
	public function uploadAvatar()
	{
		if(!$this->cms->isUserLog())
		{
			$this->cms->error('right_error','index.php');
		}
		
		require_once("core/image_upload.php");
		
		$upload = new AvatarUploader($this->cms->settings['avatar_max_x'],$this->cms->settings['avatar_max_y']);
		
		$upload->upload('avatar_img',$_SESSION['username'].'_'.dechex(time()));
		
		$avatar_url = $upload->getDestination();
		
		chmod($avatar_url,0777);
		
		$user_id = $_SESSION['user_id'];
		
		$this->cms->sql_query("UPDATE %prefix%profiles SET avatar='$avatar_url' WHERE user_id=$user_id");
	}
	
	public function avatarUploadForm()
	{
		$html = '<form method="post" enctype="multipart/form-data" action="myprofile.php?t=uploadavatar">
				<p><label for="avatar_img">Avatar : </label><input type="file" name="avatar_img" id="avatar_img" size="30"></p>
				<p><input type="submit" name="upload" value="'.$this->cms->lang['upload'].'"></p>
				</form>';
		
		$this->cms->renderHeader($this->cms->lang['profile_myprofile']);
		$this->cms->renderMenu();
		$this->cms->template->addFrame('Upload Avatar',$html);
	}
	
	public function profileForm()
	{
		require_once("core/tools/country.php");
		require_once("core/tools/date.php");
		
		$this->cms->loadStiCodeScript();
		require_once("core/sticode.php");
		
		if(!$this->cms->isUserLog())
		{
			$this->cms->error('','index.php');
		}
		
		$user_id = $_SESSION['user_id'];
		
		$dt = $this->cms->sql_query("SELECT * FROM %prefix%profiles WHERE user_id=$user_id");
		
		$data = mysql_fetch_array($dt);
		
		
		$html = '<form id="profile_form" name="profile_form" method="post" action="myprofile.php?t=update">
<fieldset>
<legend>'.$this->cms->lang['profile_general_info'].'</legend>
  <p>
    <label for="name">'.$this->cms->lang['profile_name'].' : </label>
    <input name="name" type="text" id="name" value="'.$data['profile_name'].'" tabindex="10" size="24" maxlength="32" />
    </p><p>
    <label for="avatar">'.$this->cms->lang['profile_avatar_url'].' : </label>
    <input name="avatar" type="text" id="avatar" value="'.$data['avatar'].'" tabindex="20" size="35" maxlength="64" />
    <br />
    <a href="myprofile.php?t=uploadform">'.$this->cms->lang['profile_avatar_upload_link'].'</a>
    <br />
    '.$this->cms->lang['profile_avatar_preview'].' : <br />'.ShowAvatar($data['avatar'],$this->cms->settings).'
  </p>
    <p>
    <label for="email">'.$this->cms->lang['profile_email'].' : </label>
    <input name="email" type="text" id="email" value="'.$data['email'].'" tabindex="30" size="30" maxlength="64" />
    </p>
    <p>
    <label for="msn">'.$this->cms->lang['profile_msn_im'].' : </label>
    <input name="msn" type="text" id="msn" value="'.$data['msn'].'" tabindex="40" size="30" maxlength="64" />
    </p>
    <p>
    <label for="website">'.$this->cms->lang['profile_website'].' : </label>
    <input name="website" type="text" id="website" value="'.$data['website'].'" tabindex="50" size="28" maxlength="64" />
    </p>
    '.StiCodeHTMLFormBar('forum_signature').'
    <p>
      <label for="forum_signature">'.$this->cms->lang['profile_forum_signature'].' : </label><br />
      <textarea name="forum_signature" id="forum_signature" cols="45" rows="5" tabindex="60">'.$data['forum_signature'].'</textarea>
    </p>
    
  </fieldset>
  <fieldset>
    <legend>'.$this->cms->lang['profile_personnal_info'].'</legend>
     <p>
    <label for="country">'.$this->cms->lang['profile_country'].' : </label>
    '.countryField($countryList,$data['country']).'
    </p>
     <p>
    <label for="city">'.$this->cms->lang['profile_city'].' : </label>
    <input type="text" name="city" id="city" value="'.$data['city'].'" tabindex="80" />
    </p>
     <p>
    <label for="work">'.$this->cms->lang['profile_work'].' : </label>
    <input type="text" name="work" id="work" value="'.$data['work'].'" tabindex="90" />
    </p>
        <p>
    <label for="birthdate">'.$this->cms->lang['profile_birthdate'].' : </label>
   	'.dateField($data['birthdate']).'
    </p>
    </fieldset>
  <input name="send" type="submit" value="'.$this->cms->lang['save'].'" id="send" />
</form>';
		
		$this->cms->renderHeader($this->cms->lang['profile_myprofile']);
		$this->cms->renderMenu();
		
		$this->cms->template->addFrame($this->cms->lang['profile_myprofile'],$html);
	}
	
}



?>